/**
 * UMD格式的微前端入口文件
 * 解决qiankun与Vite ES模块的兼容性问题
 */
(function () {
  console.log('[UMD Entry] Dashboard微应用UMD入口加载中...');

  let app = null;

  // qiankun生命周期函数
  window.bootstrap = async function bootstrap() {
    console.log('[UMD Entry] bootstrap');
  };

  window.mount = async function mount(props) {
    console.log('[UMD Entry] mount with props:', props);

    try {
      // 动态导入ES模块
      const { createMicroApp } = await import('../../../../../../../../src/main.ts');
      app = await createMicroApp(props);
      console.log('[UMD Entry] ✅ 微应用挂载成功');
    } catch (error) {
      console.error('[UMD Entry] ❌ 微应用挂载失败:', error);
      throw error;
    }
  };

  window.unmount = async function unmount() {
    console.log('[UMD Entry] unmount');
    try {
      if (app && app.unmount) {
        await app.unmount();
      }
      app = null;
      console.log('[UMD Entry] ✅ 微应用卸载成功');
    } catch (error) {
      console.error('[UMD Entry] ❌ 微应用卸载失败:', error);
    }
  };

  console.log('[UMD Entry] ✅ qiankun生命周期函数已挂载到window');

  // 注意：独立模式的处理由main.ts负责，这里只负责暴露qiankun生命周期函数
})();
